package com.ornament.app.web;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;

import com.ornament.app.domain.Album;
import com.ornament.app.domain.Image;
import com.ornament.app.domain.Loginuser;

/**
 * A central place to register application converters and formatters. 
 */
@Configurable
public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

	@Override
	protected void installFormatters(FormatterRegistry registry) {
		super.installFormatters(registry);
		// Register application converters and formatters
	}

	public Converter<Album, String> getAlbumToStringConverter() {
        return new org.springframework.core.convert.converter.Converter<com.ornament.app.domain.Album, java.lang.String>() {
            public String convert(Album album) {
                return new StringBuilder().append(album.getName()).toString();
            }
        };
    }

	public Converter<Integer, Album> getIdToAlbumConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.Integer, com.ornament.app.domain.Album>() {
            public com.ornament.app.domain.Album convert(java.lang.Integer id) {
                return Album.findAlbum(id);
            }
        };
    }

	public Converter<String, Album> getStringToAlbumConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.String, com.ornament.app.domain.Album>() {
            public com.ornament.app.domain.Album convert(String id) {
                return getObject().convert(getObject().convert(id, Integer.class), Album.class);
            }
        };
    }

	public Converter<Image, String> getImageToStringConverter() {
        return new org.springframework.core.convert.converter.Converter<com.ornament.app.domain.Image, java.lang.String>() {
            public String convert(Image image) {
                return new StringBuilder().append(image.getName()).toString();
            }
        };
    }

	public Converter<Integer, Image> getIdToImageConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.Integer, com.ornament.app.domain.Image>() {
            public com.ornament.app.domain.Image convert(java.lang.Integer id) {
                return Image.findImage(id);
            }
        };
    }

	public Converter<String, Image> getStringToImageConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.String, com.ornament.app.domain.Image>() {
            public com.ornament.app.domain.Image convert(String id) {
                return getObject().convert(getObject().convert(id, Integer.class), Image.class);
            }
        };
    }

	public Converter<Loginuser, String> getLoginuserToStringConverter() {
        return new org.springframework.core.convert.converter.Converter<com.ornament.app.domain.Loginuser, java.lang.String>() {
            public String convert(Loginuser loginuser) {
                return new StringBuilder().append(loginuser.getUsername()).append(" ").append(loginuser.getPassword()).toString();
            }
        };
    }

	public Converter<Integer, Loginuser> getIdToLoginuserConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.Integer, com.ornament.app.domain.Loginuser>() {
            public com.ornament.app.domain.Loginuser convert(java.lang.Integer id) {
                return Loginuser.findLoginuser(id);
            }
        };
    }

	public Converter<String, Loginuser> getStringToLoginuserConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.String, com.ornament.app.domain.Loginuser>() {
            public com.ornament.app.domain.Loginuser convert(String id) {
                return getObject().convert(getObject().convert(id, Integer.class), Loginuser.class);
            }
        };
    }

	public void installLabelConverters(FormatterRegistry registry) {
        registry.addConverter(getAlbumToStringConverter());
        registry.addConverter(getIdToAlbumConverter());
        registry.addConverter(getStringToAlbumConverter());
        registry.addConverter(getImageToStringConverter());
        registry.addConverter(getIdToImageConverter());
        registry.addConverter(getStringToImageConverter());
        registry.addConverter(getLoginuserToStringConverter());
        registry.addConverter(getIdToLoginuserConverter());
        registry.addConverter(getStringToLoginuserConverter());
    }

	public void afterPropertiesSet() {
        super.afterPropertiesSet();
        installLabelConverters(getObject());
    }
}
